//
//  InLoginViewController.m
//  Inner  Voice
//
//  Created by 逗比小胖！ on 16/3/8.
//  Copyright © 2016年 徐海宇. All rights reserved.
//

#import "InLoginViewController.h"
#import "InZhuCeViewController.h"
#import "UserModel.h"
#import <MBProgressHUD.h>
#import "NSString+Password.h"
#import "UIImage+ResizeImage.h"
#import "InPhoneViewController.h"
#import "InManagementViewController.h"

@interface InLoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;

@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
//菊花
@property (nonatomic,strong)MBProgressHUD *hud;
// 模型数组
@property(nonatomic,strong)NSMutableArray *userArray;
// 正确的密码
@property (nonatomic, copy)NSString *currentPwd;

@end

@implementation InLoginViewController

//MD5 方法加密
-(NSString *)passwordSecure {
    
    return [self.passwordTextField.text MD5];
}
//懒加载模型数组
-(NSMutableArray *)userArray {
    if (!_userArray) {
        _userArray = [NSMutableArray array];
    }
    return _userArray;
}

#pragma mark - 返回
- (IBAction)returnBtnClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setHidden:YES];
    // Do any additional setup after loading the view from its nib.
}

#pragma mark - 登录
- (IBAction)loginBtnClick:(id)sender {
    if([self.phoneTextField.text isEqualToString:@""]||[self.passwordTextField.text isEqualToString:@""]){
        [self showAlertView:@"用户名或密码不能为空"];
        return;
    }
    if ([AppDelegate shareAppdelegate].canConnectedToTheInternet) {

    //设置发送请求的网络路径
    NSString *urlPath = @"http://1.wangtest11.applinzi.com/home/Banner/login";
    //找出单例sessionManager
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
    //定义一个请求头字典
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    //将用户输入的信息传入请求头键对用的值   info 就是name
    parameters[@"info"] = self.phoneTextField.text;
    parameters[@"password"] = self.passwordTextField.text;
//    加密
    self.currentPwd = [self passwordSecure];
    //sessionManager发送POST请求
    [sessionManager POST:urlPath parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //如果成功登录
        NSLog(@"登录成功%@",responseObject);
        
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSDictionary *dic = [NSDictionary dictionaryWithDictionary:responseObject];
            
            [self loginStatusWithDic:dic];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            NSLog(@"登陆失败%@",error);
        }
    }];
    }else {
        [self showAlertView:@"当前网络不可用"];
    }

}
#pragma mark - 登录成功后执行
- (void)loginStatusWithDic:(NSDictionary *)dic {
    if ([dic[@"code"] isEqualToString:@"1"]) {
        
        UserModel *user = [[UserModel alloc] initWithDict:dic[@"data"]];
        [AppDelegate shareAppdelegate].userModel =user;
//        保存失败
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        [userDefault removeObjectForKey:@"name"];
        [userDefault setObject:user.name forKey:@"name"];
        [userDefault removeObjectForKey:@"icon"];
        [userDefault setObject:user.icon forKey:@"icon"];
        [userDefault removeObjectForKey:@"u_id"];
        [userDefault setObject:user.tel forKey:@"u_id"];
        NSLog(@"%@",user.tel);
        //登陆成功后跳回到主界面
        [self dismissViewControllerAnimated:YES completion:nil];

//        [self.navigationController popToRootViewControllerAnimated:YES];
        
    }else {
        self.currentPwd = nil;
        self.passwordTextField.text = @"";
        [self showAlertView:dic[@"data"]];
    }
}

#pragma mark - 注册
- (IBAction)zhuCeBtnClick:(id)sender {
    if ([AppDelegate shareAppdelegate].canConnectedToTheInternet) {

    self.phoneTextField.text = @"";
    self.passwordTextField.text = @"";
    InPhoneViewController *contro = [[InPhoneViewController alloc]init];
    [self presentViewController:contro animated:YES completion:nil];
    }
}
#pragma mark - 封装UIAlertView方法，直接调用就可以显示提示
-(void)showAlertView:(NSString *)string {
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:string delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
